/**
 * 
 */
package cn.com.checne.util.images;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import cn.com.checne.util.RegexUtils;

/**
 * 
 * 正文中的图片处理
 * @author zy
 *
 */
public class ImageContentUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

	/**
	 * 移除正文内容中，图片地址中包含的basePath
	 * @param content
	 * @param request
	 * @return
	 */
	public static String removeImageBasePathForString(String content,HttpServletRequest request){
		if(StringUtils.isBlank(content)){
			return content ;
		}
		String contentResult = content ;
        String firstImage = "";
        String srcRegex = "src\\s*=\\s*[\"|'](.+?[jpg|gif|png|bmp|ico])[\"|']";
        List<String> allImages = RegexUtils.getStringGoup1(content, srcRegex);
        //图片倒序排列
        /*List<String> reverseOrder = new ArrayList<String>();
        for (int i = allImages.size() - 1; i > -1; i--) {
            reverseOrder.add(allImages.get(i));
        }*/
        String basePath = request.getContextPath();
        for (String image : allImages) {
            if (!image.toLowerCase().startsWith("http")) {
                //图片类似/userfiles/images/abc.jpg 或者basePath/userfiles/images/abc.jps 最终取出userfiles/images/abc.jpg
               if(StringUtils.isBlank(basePath)){
            	   firstImage = image.substring(1);
                   contentResult = contentResult.replace(image, firstImage);
               }
               else{
            	   firstImage = image.substring(basePath.length() + 1);
                   contentResult = contentResult.replace(image, firstImage);
               }
            } else {
                firstImage = image;
            }
        }
        return contentResult ;
	}
	
	/**
	 * 移除正文内容中，图片地址中包含的basePath(创建、更新操作)
	 * @param request
	 * @param content
	 * @return Map<String,String>  
	 * 
	 * key=content
	 * key=firstImage
	 */
	public static Map<String,String> removeImageBasePathForMap(HttpServletRequest request,String content){
		Map<String,String> map = new HashMap<String,String>();
		if(StringUtils.isBlank(content)){
			return map ;
		}
        String firstImage = "";
		String contentResult = content ;
        String srcRegex = "src\\s*=\\s*[\"|'](.+?[jpg|gif|png|bmp|ico])[\"|']";
        List<String> allImages = RegexUtils.getStringGoup1(content, srcRegex);
        //图片倒序排列
        /*List<String> reverseOrder = new ArrayList<String>();
        for (int i = allImages.size() - 1; i > -1; i--) {
            reverseOrder.add(allImages.get(i));
        }*/
        String basePath = request.getContextPath();
        for (String image : allImages) {
            if (!image.toLowerCase().startsWith("http")) {
                //图片类似/userfiles/images/abc.jpg 或者basePath/userfiles/images/abc.jps 最终取出userfiles/images/abc.jpg
               if(StringUtils.isBlank(basePath)){
            	   firstImage = image.substring(1);
                   contentResult = contentResult.replace(image, firstImage);
               }
               else{
            	   firstImage = image.substring(basePath.length() + 1);
                   contentResult = contentResult.replace(image, firstImage);
               }
            } else {
                firstImage = image;
            }
        }
        
        map.put("content", contentResult);
        map.put("firstImage", firstImage);
        return map ;
	}
	
	/**
	 * 用户更新显示内容的时候，把正文中的图片地址统一添加basePath前缀
	 * @param request
	 * @param content
	 * @return
	 */
	public static String setBasePathToImagePath(HttpServletRequest request,String content){
		if(StringUtils.isBlank(content)){
			return content ;
		}
        String firstImage = "";
		String contentResult = content ;

        String srcRegex = "src\\s*=\\s*[\"|'](.+?[jpg|gif|png|bmp|ico])[\"|']";
        List<String> allImages = RegexUtils.getStringGoup1(content, srcRegex);
        //图片倒序排列
       /* List<String> reverseOrder = new ArrayList<String>();
        for (int i = allImages.size() - 1; i > -1; i--) {
            reverseOrder.add(allImages.get(i));
        }*/
        String basePath = request.getContextPath();
        for (String image : allImages) {
            if (!image.toLowerCase().startsWith("http")) {
                /**
                 * 图片格式为userfiles/images/abc.jpg
                 * 根目录变成/userfiles/images/abc.jpg
                 * 非根目录basePath/userfiles/images/abc.jpg
                 */
                if(StringUtils.isNotBlank(basePath)){
                    firstImage = basePath + "/" + image;
                    contentResult = contentResult.replace(image, firstImage);
                }
                else{
                    firstImage =  "/" + image;
                    contentResult = contentResult.replace(image, firstImage);
                }
                
            } else {
                firstImage = image;
            }
        }
        return contentResult ;
	}
}
